package LeetCode.leetcode28;

public class Main {
    /**
     * 自写算法
     * @param haystack
     * @param needle
     * @return
     */
    public int strStr(String haystack, String needle) {
        if (needle.length() > haystack.length())
            return -1;
        if (needle.equals(""))
            return 0;
        int left = 0;
        int right = haystack.length()-1;
        int last = haystack.length() - needle.length();
        int hCur = left;//
        int nCur = 0;
        while (left <= last){
            nCur = 0;
            hCur = left;
            for ( ; nCur < needle.length(); nCur++) {
                if (haystack.charAt(hCur) == needle.charAt(nCur)){
                    hCur++;
                }else {
                    break;
                }
            }
            //如果找到
            if (nCur == needle.length()){
                return left;
            }
            left++;
        }
        return -1;
    }
}
